package com.swyc.osschat.Test.jsoup;

import com.swyc.osschat.Common.Util.File.VideoTagUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description 解析网页
 * @Author: z156940l@outlook.com
 * @className Test
 * @Date: 2022/8/4 16:36
 */
public class Test {

    public static void main(String[] args) throws Exception {
        String url = "https://haokan.baidu.com/?sfrom=baidu-top";
//        Set<String> allATags = findAllATags(url);
//        allATags.forEach(System.out::println);
//        Set<String> allImages = findAllImages(url);
//        allImages.forEach(System.out::println);
        List<String> allVideos = findAllVideos(url);
        allVideos.forEach(System.out::println);
    }

    /**
     * 获取所有的超链接
     * @param url
     * @return
     */
    private static Set<String> findAllATags(String url) throws Exception{
        Set<String> urlList = new HashSet<>();
        String httpUrl = "http";
        Document document = findDom(url);
        Elements eles = document.getAllElements();
        // 遍历所有的文档
        for (Element ele : eles) {
            String tagName = ele.tagName();
            if ("a".equalsIgnoreCase(tagName)) {
                String href = ele.attr("abs:href");
                if (href.startsWith(httpUrl)) {
                    // 链接地址
                    urlList.add(href);
                }
            }
        }
        return urlList;
    }

    /**
     * 获取所有的图片地址
     * @param url
     * @return
     * @throws Exception
     */
    private static Set<String> findAllImages(String url) throws Exception{
        Set<String> pics = new HashSet<>();
        String img = "";
        Pattern p_image;
        Matcher m_image;
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile
                (regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(findDom(url).html());
        while (m_image.find()) {
            // 得到<img />数据
            img = m_image.group();
            // 匹配<img>中的src数据
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                pics.add(m.group(1));
            }
        }
        return pics;
    }

    /**
     * 获取所有的视频地址
     * @param url
     * @return
     * @throws Exception
     */
    private static List<String> findAllVideos(String url) throws Exception{
        String html = findDom(url).html();
        List<String> list = VideoTagUtils.listVideoSrc(html);
        for (String str : list) {
            System.out.println(str);
        }
        return list;
    }

    /**
     * 解析网页
     * @param url
     * @return
     */
    private static Document findDom(String url) throws Exception{
        return Jsoup.parse(new URL(url), 3000);
    }
}
